package com.example.archive.controller;

import com.example.archive.entity.*;
import com.example.archive.service.*;
import com.example.archive.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据可视化与报表控制器
 * 提供系统各模块的统计数据
 * 用于仪表板数据展示和图表生成
 */
@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin
public class DashboardController {
    
    @Autowired
    private ArchiveService archiveService;
    
    @Autowired
    private NoticeService noticeService;
    
    @Autowired
    private TrainingService trainingService;
    
    @Autowired
    private ArchiveModificationRequestService modificationRequestService;
    
    @Autowired
    private SystemLogService systemLogService;
    
    /**
     * 获取仪表板统计数据
     * 统计各模块的总数和状态分布
     * 用于首页数据展示和图表生成
     * 
     * @return 统一响应格式，包含各模块统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getDashboardStatistics() {
        try {
            Map<String, Object> statistics = new HashMap<>();
            
            // 档案统计
            long totalArchives = archiveService.count();
            long activeArchives = archiveService.lambdaQuery().eq(Archive::getStatus, "active").count();
            System.out.println("档案统计 - 总数: " + totalArchives + ", 活跃: " + activeArchives);
            statistics.put("totalArchives", totalArchives);
            statistics.put("activeArchives", activeArchives);
            
            // 公告统计
            long totalNotices = noticeService.count();
            long publishedNotices = noticeService.lambdaQuery().eq(Notice::getStatus, "published").count();
            System.out.println("公告统计 - 总数: " + totalNotices + ", 已发布: " + publishedNotices);
            statistics.put("totalNotices", totalNotices);
            statistics.put("publishedNotices", publishedNotices);
            
            // 培训资料统计
            long totalTrainings = trainingService.count();
            long publishedTrainings = trainingService.lambdaQuery().eq(Training::getStatus, "published").count();
            System.out.println("培训资料统计 - 总数: " + totalTrainings + ", 已发布: " + publishedTrainings);
            statistics.put("totalTrainings", totalTrainings);
            statistics.put("publishedTrainings", publishedTrainings);
            
            // 修改申请统计 - 详细状态统计
            long totalModificationRequests = modificationRequestService.count();
            long pendingModificationRequests = modificationRequestService.lambdaQuery().eq(ArchiveModificationRequest::getStatus, "pending").count();
            long approvedModificationRequests = modificationRequestService.lambdaQuery().eq(ArchiveModificationRequest::getStatus, "approved").count();
            long rejectedModificationRequests = modificationRequestService.lambdaQuery().eq(ArchiveModificationRequest::getStatus, "rejected").count();
            long completedModificationRequests = modificationRequestService.lambdaQuery().eq(ArchiveModificationRequest::getStatus, "completed").count();
            
            System.out.println("修改申请统计:");
            System.out.println("  总数: " + totalModificationRequests);
            System.out.println("  待审批: " + pendingModificationRequests);
            System.out.println("  已批准: " + approvedModificationRequests);
            System.out.println("  已拒绝: " + rejectedModificationRequests);
            System.out.println("  已完成: " + completedModificationRequests);
            
            statistics.put("totalModificationRequests", totalModificationRequests);
            statistics.put("pendingModificationRequests", pendingModificationRequests);
            statistics.put("approvedModificationRequests", approvedModificationRequests);
            statistics.put("rejectedModificationRequests", rejectedModificationRequests);
            statistics.put("completedModificationRequests", completedModificationRequests);
            
            // 系统日志统计
            long totalSystemLogs = systemLogService.count();
            System.out.println("系统日志统计 - 总数: " + totalSystemLogs);
            statistics.put("totalSystemLogs", totalSystemLogs);
            
            System.out.println("最终统计数据: " + statistics);
            return Result.success(statistics);
        } catch (Exception e) {
            System.err.println("获取统计数据时发生异常: " + e.getMessage());
            e.printStackTrace();
            return Result.fail(500, "获取失败：" + e.getMessage());
        }
    }
} 